<?php

namespace App\Http\Controllers\Admin\Sys;

use App\Http\Models\Admin\File\FileContent;
use App\Http\Models\Admin\Sys\SysParam;
use App\Http\Responses\ResponseDto;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Psy\Util\Json;



class SysparamController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        //
        return View::make('admin.sys.sysparam')
            ->withUrltitle(trans("admin/message.sysparam_index"))
            ->withAssets($request['ASSETS']);
    }

    public function seoshow(Request $request){

        $seos = SysParam::where("type",Config::get('common.system_type_seo'))->get();
        $seo = array();
        if($seos){
            foreach($seos as $item){
                $seo[$item->name] = $item->value;
            }
        }
        return View::make('admin.sys.seo')
            ->withUrltitle(trans("admin/message.seo_index"))
            ->withSeo((object)$seo)
            ->withAssets($request['ASSETS']);
    }



    //配置统计代码

    public function async(Request $request){

        $async = SysParam::where("name",Config::get('common.system_async_config'))->first();

        return View::make('admin.sys.async')
            ->withUrltitle(trans("admin/message.async_index"))
            ->withAsync($async? $async->value:"")
            ->withAssets($request['ASSETS']);
    }
    public function saveasync(Request $request){

        $value = $request->input("value");
        $sp = new SysParam();
        $sp->name = Config::get('common.system_async_config');
        $asyncconfig = SysParam::where("name",Config::get('common.system_async_config'))->first();
        if($asyncconfig){
            $sp = $asyncconfig;
        }
        $sp->value = $value;
        if($sp->save()){
            return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
        }else{
            return ResponseDto::createResObj(trans("admin/message.server_fail"),ResponseDto::$FAIL);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    public function page(Request $request)
    {
        $datas = $request->input();
        $start = $datas[3]['value'];
        $length = $datas[4]['value'];
//     var_dump($datas[3]['value'],$datas[4]['value'],$length*(1+$start));
        //$posts = DB::table('tags')->paginate($length)->toArray();
        $posts = DB::table('sys_param')->skip($start)->take($length)->get();
        $total = DB::table('sys_param')->count();
        return ResponseDto::createResObj(ResponseDto::convertPageToDataTableObj($posts,$total),ResponseDto::$SUCCESS);
    }

    public function saveSeo(Request $request)
    {
        foreach($request->input() as $key => $value)
        {
            if($key == "ASSETS" || substr($key, 0, 5) !="site_" ){
                continue;
            }
            $find = SysParam::where('name',$key)->first();
            if($find){
                $find->value = $value;
                $find->save();
            }else{
                $nitem = new SysParam();
                $nitem->name = $key;
                $nitem->value = $value;
                $nitem->type = Config::get('common.system_type_seo');
                $nitem->save();
            }

        }
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);

    }
    public function store(Request $request)
    {
        //
        $validator = Validator::make(Input::all(),
            [
                'name' => 'required',
                'value' => 'required',
            ]);
        if($validator->fails()){
            return ResponseDto::createResObj($validator->errors()->first(),ResponseDto::$FAIL);
        }else{
            $sysparam = new SysParam();
            $sysparam->name = $request->input("name");
            $sysparam->value = $request->input("value");
            $sysparam->desc = $request->input("desc");

            if($request->input("id")){
                //更新操作
                $exit_item = SysParam::find($request->input("id"))->first();
                if($exit_item == null){
                    return ResponseDto::createResObj(trans("admin/message.data_no_exit"),ResponseDto::$FAIL);
                }
                else{
                    $exit_item->value = $sysparam->value;
                    $exit_item->desc = $sysparam->desc;
                    $exit_item->save();
                    return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
                }
            }
            else{
                //添加操作
                $find = SysParam::where('name',$request->input("name"))->first();
                if($find){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
                $sysparam->save();
                return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
            }

        }


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        return Json::encode(SysParam::find($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
//        var_dump($id);
        //
        SysParam::destroy($id);
        return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);

    }
}
